<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Kong extends __Kong{
	
	function __construct() {

		parent::__construct();

		$this->load->config("member", $this->config);

		$this->load->module( "mysql" , $this->db );
		$this->load->module( "input" , $this->input );

		$this->load->model( "admin/admin" , $this->admin );
		
		$this->data->title = $this->data->kong->name." - ".$this->data->title;

	}
	function doit($args) {
		
		// Action!
		if ($this->input->post_exists("action")) {
			// kong Class.		
			$this->load->kong("proc");
			$this->proc = new Kong_Proc();
			$this->proc->doit();
		}

		$stat = "list";
		if ( !empty($args) ) $stat = array_shift($args);
		
		$this->data->status = "member"; // 기본 메뉴.
		
		if ($stat === "list") $this->viewList();
		else if ($stat === "new") $this->viewNew();
		else if ($stat === "group") {
			$this->data->status = "group";
			
			$gstat = "list";
			if ( !empty($args) ) $gstat = array_shift($args);
			
			if ($gstat === "list") $this->viewGroupList();
			else if ($gstat === "new") $this->viewGroupNew();
			else $this->viewGroupModify( $gstat );
			
		}
		else if ($stat === "email") {
			$this->data->status = "email";
			$this->viewEmail();
		}
		else if ($stat === "configure") {
			$this->data->status = "configure";
			$this->viewConfigure();
		}
		else $this->viewModify( $stat );
		
	}

	function viewList() {
		
		$page = $this->input->get("page", 1);
		$len_line = 20;
		
		$search		= $this->input->get("search", null);
		$keyword	= $this->input->get("keyword", null);
		
		$where = "";
		if ( !is_null($search) ) {
			switch ($search) {
				case "id" :
					$where = " where `id` like '%{$keyword}%'";
					break;
				case "name" :
					$where = " where `nick` like '%{$keyword}%'";
					break;
				case "phone" : 
					$where = " where `phone` like '%{$keyword}%'";
					break;
				case "email" : 
					$where = " where `email` like '%{$keyword}%'";
					break;	
			}
		}

		$len = $this->db->count("select count(*) from `wd_member`{$where}");
		$rows = $this->db->fetch_assoc("select * from `wd_member`{$where} order by `idx` desc limit ".($page-1)*$len_line.", {$len_line}");

		$cnt = count($rows);
		for ($i = 0; $i < $cnt; $i++) {
			$rows[$i]['n'] = $len - $i - ($page-1)*$len_line;
			$rows[$i]['options'] = @unserialize($rows[$i]['options']);
		}
		unset($cnt);

		$pagination = $this->admin->makePagination($len, array(
			"page" => $page,
			"len_line" => $len_line
		));

		$this->data->rows = $rows;
		$this->data->pagination = $pagination;

		// -- 끝.
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/list");
		$this->load->kong("view/footer");
		$this->_footer();
	
	}

	function viewNew() {

		$this->data->grouplist	= $this->db->fetch_assoc("select * from `wd_group`");
		$this->data->use_jumin	= $this->config['form']['jumin'];
		$this->data->use_phone	= $this->config['form']['phone'];
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/new");
		$this->load->kong("view/footer");
		$this->_footer();
			
	}
	function viewModify( $idx ) {

		$idx = (int)$idx;
			
		$this->load->model( 'twitter/bootstrap_form', $this->form );
		$result = $this->db->result("select * from `wd_member` where `idx`=?", array($idx));
		$result['options'] = @unserialize($result['options']);
				
		$result['groups'] = explode(",", $result['groups']);

		if ($result['options'] !== false)
			$this->data->options = implode(",", array_keys($result['options']));

		$this->data->result = $result;
		$this->data->grouplist = $this->db->fetch_assoc("select * from `wd_group`");
		$this->data->use_jumin	= $this->config['form']['jumin'];
		$this->data->use_phone	= $this->config['form']['phone'];
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/modify");
		$this->load->kong("view/footer");
		$this->_footer();
			
			
	}
	function viewGroupList() {

		$page = $this->input->get("page", 1);
		$len_line = 20;
		
		$len = $this->db->count("select count(*) from `wd_group`");
		$rows = $this->db->fetch_assoc("select * from `wd_group` order by `idx` desc limit ".($page-1)*$len_line.", {$len_line}");

		$cnt = count($rows);
		for ($i = 0; $i < $cnt; $i++) {
			$rows[$i]['n'] = $len - $i - ($page-1)*$len_line;
			$rows[$i]['options'] = @unserialize($rows[$i]['options']);
		}
		unset($cnt);

		$pagination = $this->admin->makePagination($len, array(
			"page" => $page,
			"len_line" => $len_line
		));

		$this->data->rows = $rows;
		$this->data->pagination = $pagination;
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/group_list");
		$this->load->kong("view/footer");
		$this->_footer();
	
	}

	function viewGroupNew() {
	
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/group_new");
		$this->load->kong("view/footer");
		$this->_footer();
			
	}

	function viewGroupModify( $idx ) {

		$idx = (int)$idx;

		$this->load->model( 'twitter/bootstrap_form', $this->form );
		$result = $this->db->result("select * from `wd_group` where `idx`=?", array($idx));
		
		try {
			if ( empty($result) ) throw new Error("잘못된 접근입니다.");
		}
		catch (Error $e) {echo $e; exit;}
		
		$this->data->result	= $result;

		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/group_modify");
		$this->load->kong("view/footer");
		$this->_footer();
			
	}
	function viewEmail() {

		$this->data->grouplist	= $this->db->fetch_assoc("select * from `wd_group`");
		$this->data->admin_name	= $this->config['admin']['name'];
		$this->data->admin_email= $this->config['admin']['email'];

		$this->data->admin->redactor = true;
		
		
		$this->_header();
		$this->load->kong("view/header");
		$this->load->kong("view/email");
		$this->load->kong("view/footer");
		$this->_footer();
	}
	function viewConfigure() {
		$this->_header();
		$this->load->kong("view/header");
		//$this->load->kong("view/new");
		$this->load->kong("view/footer");
		$this->_footer();
	}
	
}